﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Kernel
{
    public partial class PrjFileViewer : UserControlEx,IControlEx
    {
        public PrjFileViewer()
        {
            InitializeComponent();

            UI_Init();
        }
        
        public Project Project { set { _prj = value; } }

        private Project _prj = new Project();

        public void UI_Init()
        {
            this.listView_main.FullRowSelect = true;

            string[] v_names = { "No", "FileName", "FileType"};
            string[] v_headerTexts = { "序号", "文件名", "文件类型", "属类","可视", "编辑",
        "删除","排序","使能","描述","创建日期","更新日期","有效" };
            int[] v_widths = { 60, 320, 100, 140, 90, 90, 90, 90, 90, 90, 220, 140, 140, 90 };
            bool[] v_visables = { false, true, true, true, true, true, true, true, true, true, true, true, false };
            bool[] v_readOnlys = { false, false, false, false, false, false, false, false, false, false, false, false, true };
            string[] v_types = { "tb", "tb", "tb", "tb", "cb", "cb", "cb", "tb", "cb", "tb", "tb", "tb", "cb" };

            int n = v_names.Length;
            for (int i = 0; i < n; i++)
            {
                ColumnHeader ch = new ColumnHeader();
                ch.Name = v_names[i];
                ch.Text = v_headerTexts[i];
                ch.Width = v_widths[i];
                this.listView_main.Columns.Add(ch);
            }
        }

        public void UI_Update()
        {
            this.listView_main.Items.Clear();

            int n = 0;
            foreach (xmlFile fil in _prj.ProjectConfig.Files)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = n++.ToString();
                lvi.Tag = fil;

                lvi.SubItems.Add(fil.FileName);
                lvi.SubItems.Add(fil.FileType);

                this.listView_main.Items.Add(lvi);
            }
        }

        public void Save()
        {

        }

        public void SetData( List<xmlFile> lst_xmlFil)
        {
        }

        private void Button_Click(Object sender, EventArgs ex)
        {
            Button btn = (Button)sender;
            switch (btn.Text)
            {
                case "重新导入图纸":
                    {
                        //清空文件列表
                        _prj.ProjectConfig.Files = new List<xmlFile>();

                        //读取工程文件所在目录下的所有文件
                        List<FileInfo> lst_fi = new List<FileInfo>();
                        DirectoryInfo di = _prj.PrjFileInfo.Directory;
                        string[] typs = { ".dwg", ".doc", ".xls", ".docx", ".xlsx" };
                        FileInfo[] fis = di.GetFiles();
                        foreach (FileInfo fi in fis)
                        {
                            foreach (string typ in typs)
                            {
                                if (fi.Extension.Equals(typ))
                                {
                                    lst_fi.Add(fi);
                                    break;
                                }
                            }
                        }
                        //写入工程文件列表
                        foreach (FileInfo fi in lst_fi)
                        {
                            xmlFile xmlFile = new xmlFile();
                            xmlFile.FileName = fi.Name;
                            xmlFile.FileType = fi.Extension;

                            _prj.ProjectConfig.Files.Add(xmlFile);
                        }

                        //更新界面
                        this.UI_Update();
                        _prj.IsUpdated = true;
                    }
                    break;
                case "删除图纸":
                    var lst_lvi = this.listView_main.SelectedItems;
                    foreach( ListViewItem lvi in lst_lvi)
                    {
                        xmlFile fil = lvi.Tag as xmlFile;
                        _prj.ProjectConfig.Files.Remove(fil);

                        //更新界面
                        this.UI_Update();
                        _prj.IsUpdated = true;

                    }
                    break;
            }
        }

        #region 实现IControlEx接口
        void IControlEx.Init() { }
        void IControlEx.Load() { }
        void IControlEx.Update() { UI_Update(); }
        void IControlEx.Save() { Save(); }
        bool IControlEx.Updated { get; }
        #endregion
    }
}
